package com.example.crowdfunding.service.impl;

import com.example.crowdfunding.entity.Authority;
import com.example.crowdfunding.entity.AuthorityExample;
import com.example.crowdfunding.mapper.AuthorityMapper;
import com.example.crowdfunding.service.api.AuthorityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Resource
    private AuthorityMapper authorityMapper;

    @Override
    public List<Authority> getAll() {

        return authorityMapper.selectByExample(new AuthorityExample());
    }

    @Override
    public List<Integer> getAssignedAuthById(Integer roleId) {

        return authorityMapper.getAssignedAuthById(roleId);

    }

    @Override
    public void saveRoleAuthRelationship(Map<String, List<Integer>> map) {
        // 1.获取 roleId 的值
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);
        // 2.删除旧关联关系数据
        authorityMapper.deleteOldRelationship(roleId);
        // 3.获取 authIdList
        List<Integer> authIdList = map.get("authIdArray");
        // 4.判断 authIdList 是否有效
        if (authIdList != null && authIdList.size() > 0) {
            authorityMapper.insertNewRelationship(roleId, authIdList);
        }
    }

    @Override
    public List<String> findAssignedAuthByAdminId(Integer adminId) {

        return authorityMapper.selectAssignedAuthByAdminId(adminId);
    }

}
